<template>
	<view class="app-button-button dir-left-nowrap">
		<view class="app-home">
			<app-jump-button open_type="reLaunch" form arrangement="column" url="/pages/index/index">
				<image class="app-icon" src="../../../static/image/icon/index.png"></image>
				<text class="app-text">首页</text>
			</app-jump-button>
		</view>
        <view class="app-button dir-left-nowrap" v-if="item.goods_stock == 0">
            <view class="app-but app-but-width" style="background-color: #CDCDCD;color: #fff;">
                <view class="but dir-top-nowrap">
                    <text class="out-text">已售罄</text>
                </view>
            </view>
        </view>
		<view v-else class="app-button dir-left-nowrap">
			<view class="app-but app-single-button" v-if="is_alone_buy === 1">
				<app-form-id @click.stop="turnOn(false)">
					<view class="but dir-top-nowrap">
						<text class="price">
							{{singlePrice}}
						</text>
						<text class="app-text">单独购买</text>
					</view>
				</app-form-id>
			</view>
			<view class="app-but app-pt-button"  :class="{'app-but-width': is_alone_buy === 0}"  @click="turnOn(true)">
				<app-form-id>
					<view class="but dir-top-nowrap">
						<text class="price">{{group_price}}</text>
						<text class="app-text">{{pintuan_groups[set_group_num]?pintuan_groups[set_group_num].people_num:'0'}}人拼团</text>
					</view>
				</app-form-id>
			</view>
		</view>
	</view>
</template>

<script>

    export default {
        name: 'app-bottom-button',
	    
	    props: {
            selectAttr: Object,
            selectGroupAttrId: {
                type: [String, Number]
            },
            is_alone_buy: {
                type: Number,
            },
            pintuan_groups: {
                type: Array,
	            default() {
                    return [];
	            }
            },
            level_show: {
                type: Number,
	            default() {
                    return 0;
                }
            },
            group_min_member_price: {
                type: [String,Number],
                default() {
                    return '0.00';
                }
            },
            group_price_min: {
                type: String,
                default() {
                    return '0.00';
                }
            },
            price: {
                type: String,
	            default() {
                    return '0.00';
	            }
            },
            price_member_min: {
                type: Number,
	            default() {
                    return 0;
                }
            },
		    pt: {
                type: Boolean,
		    },
            item: Object,
	    },
	    
	    data() {
            return {
                group_price: ''
            }
	    },
	    mounted() {
            this.$nextTick(() => {
                this.get_group();
            })
	    },
	    methods: {
            turnOn(bool) {
                this.$emit('click', bool);
            },
		    
		    get_group() {
                if (Object.keys(this.selectAttr).length === 0) {
                    if (this.level_show === 1) {
                        if (Number(this.group_min_member_price) === 0) {
                            this.group_price = '免费';
                        } else {
                            this.group_price = `￥${this.group_min_member_price}`;
                        }
                    } else {
                        if (Number(this.group_price_min) ===  0) {
                            this.group_price =  '免费';
                        } else {
                            this.group_price = `￥${this.group_price_min}`;
                        }
                    }
                } else {
                    if (this.level_show === 1) {
                        if (Number(this.selectAttr.price_member) === 0) {
                            this.group_price = '免费';
                        } else {
                            this.group_price = `￥${this.selectAttr.price_member}`;
                        }
                    } else {
                        if (Number(this.selectAttr.price) ===  0) {
                            this.group_price = '免费';
                        } else {
                            this.group_price = `￥${this.selectAttr.price}`;
                        }
                    }
                }
		    }
	    },
	    computed: {
            // 单价
            singlePrice() {
                console.log(this.pt);
                console.log(this.level_show);
                if (this.pt) return;
                if (Object.keys(this.selectAttr).length === 0) {
                    if (this.level_show === 1) {
                        if (this.price_member_min === 0) {
                            return '免费';
                        } else {
                            let price = this.price_member_min;
                            return `￥${price.toFixed(2)}`
                        }
                    } else {
                        if (Number(this.price) ===  0) {
                            return '免费';
                        } else {
                            return `￥${this.price}`
                        }
                    }
                } else {
                    if (this.level_show === 1) {
                        if (Number(this.selectAttr.price_member) === 0) {
                            return '免费';
                        } else {
                            return `￥${this.selectAttr.price_member}`;
                        }
                    } else {
                        if (Number(this.price) ===  0) {
                            return '免费';
                        } else {
                            return `￥${this.selectAttr.price}`
                        }
                    }
                }
            },
		    
		    set_group_num() {
                for (let i = 0; i < this.pintuan_groups.length; i++) {
                    if (this.pintuan_groups[i].id == this.selectGroupAttrId) {
                        return i;
                    }
                }
		    }
	    },
	    
	    watch: {
            selectAttr: {
                handler() {
                    console.log(this.pt);
                    console.log(this.selectAttr);
                    if (this.pt) {
                        if (Object.keys(this.selectAttr).length === 0) {
                            if (this.level_show === 1) {
                                if (Number(this.group_min_member_price) === 0) {
                                    this.group_price = '免费';
                                } else {
                                    this.group_price =  `￥${this.group_min_member_price}`;
                                }
                                
                            } else {
                                if (Number(this.group_price_min) ===  0) {
                                    this.group_price =  '免费';
                                } else {
                                    this.group_price =  `￥${this.group_price_min}`;
                                }
                            }
                            
                        } else {
                            if (this.level_show === 1) {
                                if (Number(this.selectAttr.price_member) === 0) {
                                    this.group_price =  '免费';
                                } else {
                                    this.group_price =  `￥${this.selectAttr.price_member}`;
                                }
                            } else {
                                if (Number(this.selectAttr.price) ===  0) {
                                    this.group_price =  '免费';
                                } else {
                                    this.group_price =  `￥${this.selectAttr.price}`;
                                }
                            }
                        }
                    }
                },
	            immediate: true,
	            deep: true,
            }
	    }
    }
</script>

<style scoped lang="scss">
	.app-button-button {
		height: #{110rpx};
		width: #{750rpx};
		border-top: #{1rpx} solid #e2e2e2;
		.app-home {
			width: 13%;
			height: #{110rpx};
			background-color: white;
			.app-icon {
				width: #{40rpx};
				height: #{40rpx};
			}
			.app-text {
				font-size: #{19rpx};
				color: #707070;
			}
		}
		.app-button {
			width: 87%;
			height: #{110rpx};
			
			.app-but-width {
				width: 100% !important;
			}
			
			.app-but {
				height: #{110rpx};
				width: 50%;
			}
	
			.price {
				font-size: #{28rpx};
				margin-top: #{18rpx};
			}
			.app-text {
				font-size: #{24rpx};
				margin-top: #{8rpx};
			}
			.out-text {
                line-height: #{110rpx};
            }
			.app-single-button {
				background-color: #feeeee;
				text {
					color: #ff5c5c;
				}
			}
			.app-pt-button {
				background: linear-gradient(140deg, #ff9460, #ff5c5c);
				text {
					color: white;
				}
			}
			.but {
				text-align: center;
			}
		}
	}
</style>